package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.mapper.BaseSetMapper;
import com.weixing.mall.provider.model.domain.BaseSet;
import com.weixing.mall.provider.model.dto.cms.BaseSetDto;
import com.weixing.mall.provider.model.vo.cms.BaseSetVo;
import com.weixing.mall.provider.service.IBaseSetService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 基础设置 服务实现类
 * </p>
 *
 * @author 0202
 * @since 2020-05-28
 */
@Service
public class BaseSetServiceImpl extends ServiceImpl<BaseSetMapper,BaseSet> implements IBaseSetService{

    @Override
    public List<BaseSetVo> selectBaseSetList() {
        List<BaseSet> list = list();
        List<BaseSetVo> voList = Lists.newArrayList();
        list.forEach(baseSet -> {
            BaseSetVo bsv = new BaseSetVo();
            BeanUtils.copyProperties(baseSet,bsv);
            voList.add(bsv);
        });
        return voList;
    }

    @Override
    public void updateById(BaseSetDto baseSetDto, Long uid) {
        BaseSet baseSet = new BaseSet();
        BeanUtils.copyProperties(baseSetDto,baseSet);
        baseSet.setUpdatedBy(uid);
        updateById(baseSet);
    }

    @Override
    public BigDecimal getValueByCode(String code) {
        BaseSet baseSet = getOne(new LambdaQueryWrapper<BaseSet>().eq(BaseSet::getCode,code));
        return ObjectUtil.isNotEmpty(baseSet)?baseSet.getValue():null;
    }
}
